defmodule Giom.AccountingTest do
  use Giom.DataCase

  alias Giom.Accounting

  describe "points" do
    alias Giom.Accounting.Point

    @valid_attrs %{branch_code: "some branch_code", mgmt_code: "some mgmt_code", point_code: "some point_code", point_name: "some point_name", type: "some type"}
    @update_attrs %{branch_code: "some updated branch_code", mgmt_code: "some updated mgmt_code", point_code: "some updated point_code", point_name: "some updated point_name", type: "some updated type"}
    @invalid_attrs %{branch_code: nil, mgmt_code: nil, point_code: nil, point_name: nil, type: nil}

    def point_fixture(attrs \\ %{}) do
      {:ok, point} =
        attrs
        |> Enum.into(@valid_attrs)
        |> Accounting.create_point()

      point
    end

    test "list_points/0 returns all points" do
      point = point_fixture()
      assert Accounting.list_points() == [point]
    end

    test "get_point!/1 returns the point with given id" do
      point = point_fixture()
      assert Accounting.get_point!(point.id) == point
    end

    test "create_point/1 with valid data creates a point" do
      assert {:ok, %Point{} = point} = Accounting.create_point(@valid_attrs)
      assert point.branch_code == "some branch_code"
      assert point.mgmt_code == "some mgmt_code"
      assert point.point_code == "some point_code"
      assert point.point_name == "some point_name"
      assert point.type == "some type"
    end

    test "create_point/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Accounting.create_point(@invalid_attrs)
    end

    test "update_point/2 with valid data updates the point" do
      point = point_fixture()
      assert {:ok, point} = Accounting.update_point(point, @update_attrs)
      assert %Point{} = point
      assert point.branch_code == "some updated branch_code"
      assert point.mgmt_code == "some updated mgmt_code"
      assert point.point_code == "some updated point_code"
      assert point.point_name == "some updated point_name"
      assert point.type == "some updated type"
    end

    test "update_point/2 with invalid data returns error changeset" do
      point = point_fixture()
      assert {:error, %Ecto.Changeset{}} = Accounting.update_point(point, @invalid_attrs)
      assert point == Accounting.get_point!(point.id)
    end

    test "delete_point/1 deletes the point" do
      point = point_fixture()
      assert {:ok, %Point{}} = Accounting.delete_point(point)
      assert_raise Ecto.NoResultsError, fn -> Accounting.get_point!(point.id) end
    end

    test "change_point/1 returns a point changeset" do
      point = point_fixture()
      assert %Ecto.Changeset{} = Accounting.change_point(point)
    end
  end

  describe "stocktrans" do
    alias Giom.Accounting.Stocktrans

    @valid_attrs %{doc_no: "some doc_no", price: "120.5", price_vat: "120.5", trans_date: "2010-04-17 14:00:00.000000Z", vat: "120.5"}
    @update_attrs %{doc_no: "some updated doc_no", price: "456.7", price_vat: "456.7", trans_date: "2011-05-18 15:01:01.000000Z", vat: "456.7"}
    @invalid_attrs %{doc_no: nil, price: nil, price_vat: nil, trans_date: nil, vat: nil}

    def stocktrans_fixture(attrs \\ %{}) do
      {:ok, stocktrans} =
        attrs
        |> Enum.into(@valid_attrs)
        |> Accounting.create_stocktrans()

      stocktrans
    end

    test "list_stocktrans/0 returns all stocktrans" do
      stocktrans = stocktrans_fixture()
      assert Accounting.list_stocktrans() == [stocktrans]
    end

    test "get_stocktrans!/1 returns the stocktrans with given id" do
      stocktrans = stocktrans_fixture()
      assert Accounting.get_stocktrans!(stocktrans.id) == stocktrans
    end

    test "create_stocktrans/1 with valid data creates a stocktrans" do
      assert {:ok, %Stocktrans{} = stocktrans} = Accounting.create_stocktrans(@valid_attrs)
      assert stocktrans.doc_no == "some doc_no"
      assert stocktrans.price == Decimal.new("120.5")
      assert stocktrans.price_vat == Decimal.new("120.5")
      assert stocktrans.trans_date == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")
      assert stocktrans.vat == Decimal.new("120.5")
    end

    test "create_stocktrans/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Accounting.create_stocktrans(@invalid_attrs)
    end

    test "update_stocktrans/2 with valid data updates the stocktrans" do
      stocktrans = stocktrans_fixture()
      assert {:ok, stocktrans} = Accounting.update_stocktrans(stocktrans, @update_attrs)
      assert %Stocktrans{} = stocktrans
      assert stocktrans.doc_no == "some updated doc_no"
      assert stocktrans.price == Decimal.new("456.7")
      assert stocktrans.price_vat == Decimal.new("456.7")
      assert stocktrans.trans_date == DateTime.from_naive!(~N[2011-05-18 15:01:01.000000Z], "Etc/UTC")
      assert stocktrans.vat == Decimal.new("456.7")
    end

    test "update_stocktrans/2 with invalid data returns error changeset" do
      stocktrans = stocktrans_fixture()
      assert {:error, %Ecto.Changeset{}} = Accounting.update_stocktrans(stocktrans, @invalid_attrs)
      assert stocktrans == Accounting.get_stocktrans!(stocktrans.id)
    end

    test "delete_stocktrans/1 deletes the stocktrans" do
      stocktrans = stocktrans_fixture()
      assert {:ok, %Stocktrans{}} = Accounting.delete_stocktrans(stocktrans)
      assert_raise Ecto.NoResultsError, fn -> Accounting.get_stocktrans!(stocktrans.id) end
    end

    test "change_stocktrans/1 returns a stocktrans changeset" do
      stocktrans = stocktrans_fixture()
      assert %Ecto.Changeset{} = Accounting.change_stocktrans(stocktrans)
    end
  end

  describe "ptrans" do
    alias Giom.Accounting.Ptrans

    @valid_attrs %{date: "2010-04-17 14:00:00.000000Z"}
    @update_attrs %{date: "2011-05-18 15:01:01.000000Z"}
    @invalid_attrs %{date: nil}

    def ptrans_fixture(attrs \\ %{}) do
      {:ok, ptrans} =
        attrs
        |> Enum.into(@valid_attrs)
        |> Accounting.create_ptrans()

      ptrans
    end

    test "list_ptrans/0 returns all ptrans" do
      ptrans = ptrans_fixture()
      assert Accounting.list_ptrans() == [ptrans]
    end

    test "get_ptrans!/1 returns the ptrans with given id" do
      ptrans = ptrans_fixture()
      assert Accounting.get_ptrans!(ptrans.id) == ptrans
    end

    test "create_ptrans/1 with valid data creates a ptrans" do
      assert {:ok, %Ptrans{} = ptrans} = Accounting.create_ptrans(@valid_attrs)
      assert ptrans.date == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC")
    end

    test "create_ptrans/1 with invalid data returns error changeset" do
      assert {:error, %Ecto.Changeset{}} = Accounting.create_ptrans(@invalid_attrs)
    end

    test "update_ptrans/2 with valid data updates the ptrans" do
      ptrans = ptrans_fixture()
      assert {:ok, ptrans} = Accounting.update_ptrans(ptrans, @update_attrs)
      assert %Ptrans{} = ptrans
      assert ptrans.date == DateTime.from_naive!(~N[2011-05-18 15:01:01.000000Z], "Etc/UTC")
    end

    test "update_ptrans/2 with invalid data returns error changeset" do
      ptrans = ptrans_fixture()
      assert {:error, %Ecto.Changeset{}} = Accounting.update_ptrans(ptrans, @invalid_attrs)
      assert ptrans == Accounting.get_ptrans!(ptrans.id)
    end

    test "delete_ptrans/1 deletes the ptrans" do
      ptrans = ptrans_fixture()
      assert {:ok, %Ptrans{}} = Accounting.delete_ptrans(ptrans)
      assert_raise Ecto.NoResultsError, fn -> Accounting.get_ptrans!(ptrans.id) end
    end

    test "change_ptrans/1 returns a ptrans changeset" do
      ptrans = ptrans_fixture()
      assert %Ecto.Changeset{} = Accounting.change_ptrans(ptrans)
    end
  end
end
